package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class SetmesServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void saveSetmeal(SetmealDTO setmealDTO) {

        System.out.println("请求id" + setmealDTO.getId());

        saveOrUpdate(setmealDTO);
        Integer sid = setmealDTO.getId();
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();

        for (Integer gid : checkgroupIds) {
            baseMapper.addSetmealAndGroupInfo(sid, gid);
        }
        if (setmealDTO.getImg() != null) {
            //  套餐保存成功 图片保存到redis上
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmealDTO.getImg());
        }
    }

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Page<Setmeal> page = null;
        PageResult result = null;
        QueryWrapper queryWrapper = new QueryWrapper();

        //      有条件分页查询  先判断条件是否为空
        if (StringUtils.isEmpty(pageBean.getQueryString())) {

            // mp  无分页查询     先配置 分页插件
            page = page(new Page<Setmeal>(pageBean.getCurrentPage(), pageBean.getPageSize()));

            //无条件查询结果
            result = new PageResult(page.getTotal(), page.getRecords());

            return result;
        } else {
            // 按照名称模糊查询
            queryWrapper.like("name", pageBean.getQueryString());

            //有条件查询结果
            page = page(new Page<Setmeal>(pageBean.getCurrentPage(), pageBean.getPageSize()), queryWrapper);

            result = new PageResult(page.getTotal(), page.getRecords());
        }


        return result;
    }

    //删除
    @Override
    public void deleteySetmeal(Integer id) {
        //先删除中间表 在删除Group
        try {
            baseMapper.deleteSetmealAndcheckgroup(id);
            baseMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Integer> checkGroupBySetmealId(int id) {
        //使用传递过来的检查组id  查询所有检查项
        return baseMapper.checkGroupBySetmealId(id);
    }

    @Override
    public SetmealVO findSetMealDetail(int sid) {
        //在这里需要根据套餐id查询 套餐内容  (检查组名称 检查组解读 检查项名称)

        //查询套餐
        SetmealVO setmealVO = baseMapper.selectBySetMealId(sid);

        //查询套餐需要的检查组信息
        List<CheckGroupVO> checkGroupVO = baseMapper.selectBySetMealIdAndGroup(setmealVO.getId());

        //查询检查组里包含的检查项名称  (先遍历  拿到所有检查组id)
        for (CheckGroupVO groupVO : checkGroupVO) {
            Integer gid = groupVO.getId();
            List<CheckItem> checkItemList = baseMapper.selectGroupAndCheckitemByGid(gid);
            //封装到检查组中
            groupVO.setCheckItems(checkItemList);
        }

        //把检查组封装到套餐中
        setmealVO.setCheckGroups(checkGroupVO);

        return setmealVO;
    }


    //查询套餐是否被预约
    @Override
    public int selectByUser(Integer id) {
       int i =  baseMapper.SelectByUser(id);

        return i;
    }


}

